package yunquan.wang.test.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.drools.KnowledgeBase;
import org.drools.SystemEventListenerFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.process.workitem.wsht.HornetQHTWorkItemHandler;
import org.jbpm.task.TaskService;
import org.jbpm.task.query.TaskSummary;
import org.jbpm.task.service.SyncTaskServiceWrapper;
import org.jbpm.task.service.TaskServer;
import org.jbpm.task.service.hornetq.AsyncHornetQTaskClient;
import org.jbpm.task.service.hornetq.HornetQTaskServer;
import org.junit.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.process.ProcessService;
import yunquan.wang.test.ProcessTest;
import yunquan.wang.test.ServiceTest;


/**
 * This is a sample file to launch a process.
 */
public class EvaluationExample2  extends ServiceTest{
	@Resource
	ProcessService processService;
 
	@Resource
private StatefulKnowledgeSession ksession;
	public TaskServer server = null;
	@Test
	@Transactional("jpaTransaction")
	@Rollback(false)
    public  void test() throws Exception {
 
            // start a new process instance
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("employee", "krisv");
            params.put("reason", "Yearly performance evaluation");
            ksession.startProcess("com.sample.evaluation", params);

    
            TaskService taskClient = processService.getTaskService();
    


            Thread.sleep(1000);
            // "krisv" executes his own performance evaluation
            TaskSummary task1 = taskClient.getTasksAssignedAsPotentialOwner("krisv", "en-UK").get(0);
            System.out.println("Krisv executing task " + task1.getName() + "(" + task1.getId() + ": " + task1.getDescription() + ")");

            taskClient.start(task1.getId(), "krisv");

            taskClient.complete(task1.getId(), "krisv", null);


            Thread.sleep(1000);
            // "john", part of the "PM" group, executes a performance evaluation
            List<String> groups = new ArrayList<String>();
            groups.add("PM");

            TaskSummary task2 = taskClient.getTasksAssignedAsPotentialOwner("john", groups, "en-UK").get(0);
            System.out.println("John executing task " + task2.getName() + "(" + task2.getId() + ": " + task2.getDescription() + ")");

            taskClient.claim(task2.getId(), "john", groups);

            taskClient.start(task2.getId(), "john");


            taskClient.complete(task2.getId(), "john", null);
            
            Thread.sleep(1000);
            // "mary", part of the "HR" group, delegates a performance evaluation
            groups = new ArrayList<String>();
            groups.add("HR");

            TaskSummary task3 = taskClient.getTasksAssignedAsPotentialOwner("mary", groups, "en-UK").get(0);
            System.out.println("Mary delegating task " + task3.getName() + "(" + task3.getId() + ": " + task3.getDescription() + ") to krisv");

            taskClient.claim(task3.getId(), "mary", groups);

            taskClient.delegate(task3.getId(), "mary", "krisv");

            
            // "administrator" delegates the task back to mary
            System.out.println("Administrator delegating task back to mary");

            taskClient.delegate(task3.getId(), "Administrator", "mary");

            Thread.sleep(1000);
            // mary executing the task
            TaskSummary task3b = taskClient.getTasksAssignedAsPotentialOwner("mary", "en-UK").get(0);
            System.out.println("Mary executing task " + task3b.getName() + "(" + task3b.getId() + ": " + task3b.getDescription() + ")");

            taskClient.start(task3b.getId(), "mary");


            taskClient.complete(task3b.getId(), "mary", null);

            taskClient.disconnect();
         
            Thread.sleep(1000);
            
           
            System.exit(0);
    
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("evaluation/Evaluation2.bpmn"), ResourceType.BPMN2);
        return kbuilder.newKnowledgeBase();
    }

    private static class SystemEventListener implements org.drools.SystemEventListener {

        public void debug(String arg0) {
        }

        public void debug(String arg0, Object arg1) {
        }

        public void exception(Throwable arg0) {
        }

        public void exception(String arg0, Throwable arg1) {
        }

        public void info(String arg0) {
        }

        public void info(String arg0, Object arg1) {
        }

        public void warning(String arg0) {
        }

        public void warning(String arg0, Object arg1) {
        }
    }
}
